home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / KMAGV3.ZIP / KMAGUNIT.C < prev    next >
C/C++ Source or Header  |  1996-01-11  |  10KB  |  384 lines

  1. //THE KING MAGAZINE UNIT FOR C               //
  2. //WRITING BY THE KING IN 01/02/96            //
  3. #include <stdio.h>
  4. #include <process.h>
  5. #include <dos.h>
  6. #include <conio.h>
  7. #include <mem.h>
  8.  
  9. #define FALSE        0
  10. #define TRUE         1
  11.  
  12. #define M_NONE       0
  13. #define M_LEFT       1
  14. #define M_RIGHT      2
  15. #define M_LEFTRIGHT  3
  16.  
  17. #ifdef __cplusplus
  18.     #define __CPPARGS ...
  19. #else
  20.     #define __CPPARGS
  21. #endif*/
  22.  
  23. //struct for the 3 values of the palettes.
  24. struct sRGB {
  25.     char red,green,blue;
  26. };
  27. //struct of 256 colors of the color type.
  28. typedef sRGB PaletteType [256];
  29.  
  30. //mouse struct
  31. struct mousestruct {
  32.     unsigned int x,y;
  33.     char buttons;
  34. };
  35.  
  36. //CEL format header.
  37. struct sCelHeader {
  38.     int sign;                     //sign of the .CEL format.
  39.     int w,h,x,y;                  //size and place..
  40.     char depth,compress;          //compression type..
  41.     long data;                    //size of BITMAP.
  42.     char filler[16];              //Not use...
  43.     PaletteType pal;              //The Pallette of the .CEL.
  44. };
  45.  
  46. //keyboard keys.
  47.  
  48. char keys[128];
  49. mousestruct mouse;
  50. void interrupt(*old9int)(__CPPARGS);
  51.  
  52. //-------------------Set Modes Routines----------------//
  53.  
  54. //set mode to 320x200x256 colors..
  55. void SetMode()
  56. {
  57.     asm {
  58.         mov ah,0x00
  59.         mov al,0x13
  60.         int 0x10
  61.     }
  62.  
  63. }
  64.  
  65. //set to 80x25 text mode.
  66. void SetTextMode()
  67. {
  68.     asm {
  69.         mov ah,0x00
  70.         mov al,0x03
  71.         int 0x10
  72.     }
  73.  
  74. }
  75.  
  76. //----------------------------Graphics Routines-----------------------------//
  77.  
  78. //------------------------------------------------//
  79. //Plot a single pixel on the screen .             //
  80. //------------------------------------------------//
  81. void PutPixel(int X,int Y,unsigned char Col) {
  82.     asm {
  83.     mov ax,0xa000     //Ax = SEGMENT OF THE SCREEN//
  84.     mov es,ax         //Es = SEGMENT OF THE SCREEN//
  85.     mov ax,320        //Ax = MAX VERTICAL LINE//
  86.     mul Y             //Ax = AX * Y = HORIZONTAL LINE//
  87.     add ax,X          //Ax = VERTICAL LINE + HORIZONTAL LINE = OFFSET//
  88.     mov di,ax         //DI = OFFSET//
  89.     mov al,Col        //AL = COLOR//
  90.     stosb             //[0A000h:OFFSET] = COLOR//
  91.     }
  92. }
  93.  
  94. //-----------------------------------------//
  95. // Show Picture On Screen .                //
  96. //-----------------------------------------//
  97.  
  98. void ShowPic(unsigned char *Pic)
  99. {
  100. asm {
  101.     push ds
  102.     mov ax,word(Pic+2)                //Take The Segment Of Pic
  103.     mov ds,ax
  104.     xor si,si                         //Si = 0
  105.     mov ax,0xa000
  106.     mov es,ax
  107.     xor di,di                         //Di = 0
  108.     mov cx,32000                      //32000*2 = 64000
  109.     rep movsw                         //Move 32000*2 Bytes
  110.     pop ds
  111.     }
  112. }
  113.  
  114. //------------------------------Palette Routines----------------------------//
  115.  
  116. //-------------------------------------------------------//
  117. //Get Red Green And Blue From a Color                    //
  118. //-------------------------------------------------------//
  119.  
  120. void GetColor(unsigned char Col,char &R,char &G,char &B)
  121. {
  122.     asm {
  123.     mov dx,0x3c7                  //Set To GET COLOR
  124.     mov al,Col
  125.     out dx,al
  126.     inc dx                       //Dx = 3c8H
  127.     inc dx                       //Dx = 3c9H
  128.     les di,R                     //Es:Di = R
  129.     in al,dx                     //Get Red Value
  130.     mov [es:di],al               //R = Red Value
  131.     in al,dx                     //Get Green Value
  132.     les di,G                     //Es:Di = G
  133.     mov [es:di],al               //G = Green Value
  134.     in al,dx                     //Get Blue Value
  135.     les di,B                     //Es:Di = B
  136.     mov [es:di],al               //B = Blue Value
  137.     }
  138. }
  139.  
  140. //-------------------------------------------------------//
  141. //Set Red Green And Blue To a Color                      //
  142. //-------------------------------------------------------//
  143.  
  144. void SetColor(unsigned char Col,char R,char G,char B)
  145. {
  146. asm {
  147.     mov dx,0x3c8                 //SET TO SET COLOR
  148.     mov al,Col
  149.     out dx,al
  150.     inc dx                       //DX = 3c9h
  151.     mov al,R                     //Senting Red Value
  152.     out dx,al
  153.     mov al,G                     //Senting Green Value
  154.     out dx,al
  155.     mov al,B                     //Senting Blue Value
  156.     out dx,al
  157.     }
  158. }
  159.  
  160. //---------------------------------------------------//
  161. // Show The Palette                                  //
  162. //---------------------------------------------------//
  163. void ShowPal(PaletteType Pal)
  164. {
  165. unsigned char t;
  166.  
  167.     for(t=0;t<255;t++)
  168.     {
  169.         SetColor(t,Pal[t].red,Pal[t].green,Pal[t].blue);
  170.     }
  171. }
  172.  
  173. //---------------------------------------------------//
  174. // Get The Use Palette From The Screen               //
  175. //---------------------------------------------------//
  176.  
  177. void GetPal(PaletteType Pal)
  178. {
  179. unsigned char t;
  180.     for (t=0;t<255;t++)
  181.     {
  182.         GetColor(t,Pal[t].red,Pal[t].green,Pal[t].blue);
  183.     }
  184. }
  185.  
  186. //---------------------------------------------------//
  187. // Fade To The Screen From Palette To Palette.       //
  188. //---------------------------------------------------//
  189. void FadeTo(PaletteType Pal,PaletteType ToPal)
  190. {
  191. unsigned char t,t1;
  192.     for(t1=1;t1<63;t++)
  193.     {
  194.         for(t=1;t1<255;t++)
  195.         {
  196.             if (Pal[t].red > ToPal[t].red)
  197.                 Pal[t].red--;
  198.             if (Pal[t].red < ToPal[t].red)
  199.                 Pal[t].red++;
  200.             if (Pal[t].green > ToPal[t].green)
  201.                 Pal[t].green--;
  202.             if (Pal[t].green < ToPal[t].green)
  203.                 Pal[t].green++;
  204.             if (Pal[t].blue > ToPal[t].blue)
  205.                 Pal[t].blue--;
  206.             if (Pal[t].blue < ToPal[t].blue)
  207.                 Pal[t].blue++;
  208.         }
  209.         ShowPal(Pal);
  210.     }
  211. }
  212.  
  213. //-------------------------------File Formats-------------------------------//
  214.  
  215. char LoadCel(const char *name,unsigned char *Where,PaletteType Pal)
  216. {
  217. FILE *f;
  218. sCelHeader Cel;
  219. char loaded;
  220.  
  221.     if((f = fopen(name,"rb")) != NULL) {
  222.         loaded = TRUE;
  223.         fread(&Cel,sizeof(sCelHeader)-768,1,f);
  224.         fread(Pal,sizeof(PaletteType),1,f);
  225.         fread(Where,64000,1,f);
  226.         fclose(f);
  227.     }
  228.     else
  229.     {
  230.         loaded = FALSE;
  231.     }
  232.     return(loaded);
  233. }
  234.  
  235. //---------------------------------Effects----------------------------------//
  236.  
  237. //---------------------------------------------//
  238. //Build The Picture Of The Cross Fade          //
  239. //---------------------------------------------//
  240.  
  241. void MakeCrossFade(unsigned char *Pic1,unsigned char *Pic2,
  242.                    unsigned char *Pic3,PaletteType PalP1,
  243.                    PaletteType PalP2,PaletteType PalCp1,
  244.                    PaletteType PalCp2)
  245. {
  246.     struct pixels {
  247.         unsigned char Pix1,Pix2;
  248.     };
  249.     pixels Colors[255];
  250.  
  251.     unsigned int T;
  252.     unsigned int T1;
  253.     unsigned int Word;
  254.     unsigned char Pix1,Pix2;
  255.     unsigned int Num;
  256.     T=0;
  257.     Num = 1;
  258.     do{
  259.         Pix1 = Pic1[T];
  260.         Pix2 = Pic2[T];
  261.         for(T1=0;T1<Num-1;T1++){
  262.             if ((Num != 1) && (Pix1==Colors[T1].Pix1) && (Pix2==Colors[T1].Pix2))
  263.             {
  264.                 Pic1[T] = T1;
  265.                 T1=256;
  266.                 break;
  267.             }
  268.         }
  269.  
  270.         if (T1 != 256)
  271.         {
  272.             Pic3[T] = Num;
  273.             PalCp1[Num] = PalP1[Pix1];
  274.             PalCp2[Num] = PalP2[Pix2];
  275.             Colors[Num].Pix1 = Pix1;
  276.             Colors[Num].Pix2 = Pix2;
  277.             Num ++;
  278.         }
  279.         T++;
  280.         if(Num > 255)   {
  281.             printf("More Then 256 Colors . ");
  282.             exit;
  283.         }
  284.     } while (T != 64000);
  285. }
  286. //-----------------------------Keyboard Routines------------------------------//
  287.  
  288. //--------------------------------------------//
  289. //New Interrupt 9 for handle with the keyboard//
  290. //--------------------------------------------//
  291. void interrupt new9int(__CPPARGS){
  292.     asm  {
  293.         mov ax,0x40
  294.         mov es,ax
  295.         mov di,0x17
  296.         mov ax,0
  297.         mov [es:di],ax
  298.     }
  299.     keys[inp(0x60) % 128] = (inp(0x60) < 128);
  300.     old9int();
  301.     asm {
  302.         mov al,0x20
  303.         out 0x20,al
  304.     }
  305.     asm {
  306.         mov ax,0x40
  307.         mov es,ax
  308.         mov di,0x1A
  309.         mov ds,ax
  310.         mov si,0x1c
  311.         mov ax,[ds:si]
  312.         mov [es:di],ax
  313.         }
  314. }
  315.  
  316. //-------------------------------------------//
  317. //         Init The new interrupt            //
  318. //-------------------------------------------//
  319.  
  320. void InitKeyboard(){
  321.     memset(keys,0,sizeof(keys));
  322.     old9int = getvect(0x9);
  323.     setvect(0x9,new9int);
  324. }
  325. //--------------------------------------//
  326. //      Restore The Old interrupt       //
  327. //--------------------------------------//
  328.  
  329. void RestoreKeyBoard(){
  330.     setvect(0x9,old9int);
  331. }
  332.  
  333. //------------------------------Mouse Routines------------------------------//
  334.  
  335. //--------------------------------------//
  336. //         Get the mouse status         //
  337. //--------------------------------------//
  338.  
  339. void GetMouse(mousestruct &Mouse)
  340. {
  341.     asm {
  342.         push ds                 //Saving DS
  343.         mov ax,0x0003            //Function 0003H INT 33H GET STATUS
  344.         int 0x33
  345.         lds si,Mouse            //[DS:SI] = MOUSE
  346.         shr cx,3                //FOR DIVIDE IT WITH 8
  347.         shr dx,3
  348.         mov [ds:si],cx          //[DS:SI] = X = CX
  349.         mov [ds:si+2],dx        //[DS:SI+2] = Y = DX
  350.         mov [ds:si+4],bx        //[DS:SI+4] = BUTTON = BX
  351.         pop ds                  //Restoring DS
  352.     }
  353. }
  354.  
  355. //Thus function Reseting the mouse and return true if the mouse is installed//
  356.  
  357. char ResetMouse()
  358. {
  359. char rett;
  360.     asm {
  361.         mov ax,0x0000
  362.         int 0x33
  363.         mov rett,al
  364.     }
  365.     return(rett);
  366. }
  367.  
  368. //Show the mouse on the screen//
  369. void ShowMouse(){
  370.     asm {
  371.         mov ax,0x0001
  372.         int 0x33
  373.     }
  374. }
  375.  
  376. //Hide the mouse from the screen//
  377. void HideMouse(){
  378.     asm {
  379.         mov ax,0x0002
  380.         int 0x33
  381.     }
  382. }
  383.  
  384.